package com.hilaver.dzmis.service.impl;

import java.util.List;
import java.util.Set;

import com.hilaver.dzmis.Constants;
import com.hilaver.dzmis.product.ProductOrderItem;
import com.hilaver.dzmis.product.ProductOrderItemColor;
import com.hilaver.dzmis.service.AbstractBaseService;
import com.hilaver.dzmis.util.SimpleObj2XML;
import com.hilaver.dzmis.util.StringUtils;

public class OrderServiceImpl extends AbstractBaseService {
	
	public final static String COLUMN_PI_ID = "productIdentification.id";
	
	public final static String COLUMN_COLOR = "colorNumber";
	
	public final static String COLUMN_ORDER_ITEM_ID = "productOrderItem.id";
	
	private ProductIdentificationServiceImpl ps;
	
	public OrderServiceImpl(){
		ps = new ProductIdentificationServiceImpl();
	}
	
	public String edit(ProductOrderItem obj) throws Exception {
		String [] filters = new String[1];
		filters[0] = COLUMN_ORDER_ITEM_ID + "=" + obj.getId();
		this.dao.delete(ProductOrderItemColor.class.getName(), filters);
		this.dao.saveOrUpdate(obj);
		//this.dao.storeSysMbox(obj);
		return "{success: true}";
	}

	@Override
	public String delete(int id) throws Exception {
		return super.delete(ProductOrderItem.class.getName(), id);
	}

	@Override
	public String get(int id) throws Exception {
		ProductOrderItem obj = (ProductOrderItem)this.dao.get(ProductOrderItem.class.getName(), id);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		xmlSB.append(Constants.XML_ITEM_TAG[0]);
		xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
		xmlSB.append(this.getColorItemStr(obj));
		xmlSB.append(Constants.XML_ITEM_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}

	@Override
	public String getAllPagination(int page, String sort, String order)
			throws Exception {
		// TODO Auto-generated method stub
		return null;
	}
	
	public String getAllPagination(int page, String sort,
			String order, String[] filter) throws Exception {
		this.ps.setLocale(this.locale);
		int offset = Constants.PAGE_SIZE * (page - 1);
		if("id".equals(sort)){
			sort = COLUMN_PI_ID + ", " + COLUMN_COLOR + " ";
		}
		List<ProductOrderItem> objList = this.dao.getAllPagination(ProductOrderItem.class.getName(), offset,
				Constants.PAGE_SIZE, sort, order, filter);

		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		for (ProductOrderItem obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(++offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);
			xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
			xmlSB.append(this.ps.toXMLFragment(obj.getProductIdentification()));
			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(page);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(ProductOrderItem.class.getName(), filter));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);

		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	public String getAll(Integer piId) throws Exception{
		String [] filters = new String[1];
		filters[0] = COLUMN_PI_ID + "=" + piId;
		List<ProductOrderItem> objList = this.dao.getAll(ProductOrderItem.class.getName(), COLUMN_ID, "asc", filters);
		StringBuffer xmlSB = new StringBuffer();
		xmlSB.append(Constants.XML_DOC_DEFINE);
		xmlSB.append(Constants.XML_ROOT_TAG[0]);
		int offset = 0;
		for (ProductOrderItem obj : objList) {
			xmlSB.append(Constants.XML_ITEM_TAG[0]);
			xmlSB.append(Constants.XML_INDEX_TAG[0]);
			xmlSB.append(offset);
			xmlSB.append(Constants.XML_INDEX_TAG[1]);
			xmlSB.append(SimpleObj2XML.toXMLFragment(obj));
			xmlSB.append(this.getColorItemStr(obj));
			xmlSB.append(Constants.XML_ITEM_TAG[1]);
		}
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[0]);
		xmlSB.append(Constants.XML_CURRENT_PAGE_TAG[1]);

		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[0]);
		xmlSB.append(this.dao.getCount(ProductOrderItem.class.getName(),filters));
		xmlSB.append(Constants.XML_ITEM_TOTAL_TAG[1]);
		xmlSB.append(Constants.XML_ROOT_TAG[1]);
		return xmlSB.toString();
	}
	
	private String getColorItemStr(ProductOrderItem obj){
		Set<ProductOrderItemColor> colorItems = obj.getProductOrderItemColors();
		char index = 'A';
		StringBuffer xmlSB = new StringBuffer();
		for (ProductOrderItemColor item : colorItems){
			if(!StringUtils.isEmpty(item.getColorNumber())){
				xmlSB.append("<color" + index + ">");
				xmlSB.append(Constants.XML_CDATA[0]);
				xmlSB.append(item.getColorNumber());
				xmlSB.append(Constants.XML_CDATA[1]);
				xmlSB.append("</color" + index + ">");
				
				xmlSB.append("<pantoneNumber" + index + ">");
				xmlSB.append(Constants.XML_CDATA[0]);
				xmlSB.append(item.getPantoneNumber());
				xmlSB.append(Constants.XML_CDATA[1]);
				xmlSB.append("</pantoneNumber" + index + ">");
				
				xmlSB.append("<realColor" + index + ">");
				xmlSB.append(Constants.XML_CDATA[0]);
				xmlSB.append(item.getRealColor() == null?"ffffff":item.getRealColor());
				xmlSB.append(Constants.XML_CDATA[1]);
				xmlSB.append("</realColor" + index + ">");
				
				xmlSB.append("<percentage" + index + ">");
				xmlSB.append(Constants.XML_CDATA[0]);
				xmlSB.append(item.getPercentage());
				xmlSB.append(Constants.XML_CDATA[1]);
				xmlSB.append("</percentage" + index + ">");
				
				index++;
			}
		}
		
		return xmlSB.toString();
	}

	public static void main(String[] args) throws Exception{
		OrderServiceImpl os = new OrderServiceImpl();
		System.out.println(os.getAll(2));
		//char index = 'C';
		//System.out.println(++index);
	}
}
